package com.gui;

import java.util.ArrayList;
import java.util.List;

import org.teleal.cling.android.AndroidUpnpService;
import org.teleal.cling.model.meta.RemoteDevice;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class DLNAActivity extends Activity {
    
	private static final String TAG = DLNAActivity.class.getName();
	private BrowseRegistryListener registryListener = new BrowseRegistryListener();
	private AndroidUpnpService upnpService;
	private Button btnLoad;	
	private ListView listView;
	DMSServerAdapter listAdapter;
	List<DMSServer> DMSServerList = new ArrayList<DMSServer>();
	
	private ServiceConnection serviceConnection = new ServiceConnection(){

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			upnpService = (AndroidUpnpService) service;
			registryListener.setService(upnpService);	
			upnpService.getRegistry().addListener(registryListener);
			upnpService.getControlPoint().search();
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			
			upnpService = null;
		}		
	};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		
        listAdapter = new DMSServerAdapter(this, 0, 0, DMSServerList);
        listView = (ListView) findViewById(R.id.lv_Browse);
        listView.setAdapter(listAdapter);        
		
		listView.setOnItemClickListener(listViewOnItemClickListener);
		
        btnLoad = (Button) findViewById(R.id.btnLoad);        
		btnLoad.setOnClickListener(btnOnClickListener);
        getApplicationContext().bindService(
        		new Intent(this, BrowserUpnpService.class), serviceConnection, Context.BIND_AUTO_CREATE
        );
    }
    
    OnClickListener btnOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			List<RemoteDevice> DMSServers = registryListener.showDMSServer();
			for (RemoteDevice dmsServer : DMSServers){
				DMSServer server = new DMSServer(dmsServer.getDetails().getFriendlyName(), 
													dmsServer.getDetails().getSerialNumber());
				DMSServerList.add(server);
			}			
			listAdapter.notifyDataSetChanged();
		}
	};
	
	OnItemClickListener listViewOnItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> a, View v, int i,
				long l) {
			if (i > -1){
				DMSServer server = DMSServerList.get(i);
				Log.i(TAG,"server.getSerial(): " + server.getName());
				Log.i(TAG,"server.getSerial(): " + server.getSerial());
				
			}						
		}
	};

		
}